package com.llb666.java.thread;

import java.util.concurrent.TimeUnit;

/**
 * @author 斌~
 * @version 1.0
 * @date 2021/7/13 下午10:42
 */
public class ThreadLocalDemo {
    public static void main(String[] args) {
        Person person = new Person();
        new Thread(new Runnable(){
            @Override
            public void run() {
                person.setName("张三");
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程1====="+person.getName());
            }
        }).start();
        new Thread(new Runnable(){
            @Override
            public void run() {
                person.setName("李四");
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程2====="+person.getName());
            }
        }).start();
    }

    static class Person{
        private final ThreadLocal<String> name  = new ThreadLocal<>();

        public String getName() {
            return this.name.get();
        }

        public void setName(String name) {
            this.name.set(name);
        }
    }
}
